package org.september.pisces.filestore.service.hik;





import cn.hutool.core.codec.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AuthorizationUtil {

	/**
	 * 获取Authorization
	 * @param accessKey
	 * @param secretKey
	 * @param httpVerb
	 * @param contentType
	 * @param date
	 * @param uri
	 * @param encryptionAlgrithm
	 * @return
	 */
	public static String getAuthorization(String authorizationPrefix, String accessKey,String secretKey,String httpVerb,
			String contentType,String date,String uri, int encryptionAlgrithm){
		StringBuffer auth_data = new StringBuffer();
		auth_data.append(authorizationPrefix).append(" ");
		auth_data.append(accessKey).append(":");
//md5都为空
		String signature = getSignature(secretKey,httpVerb,"",contentType,date,uri, encryptionAlgrithm);
		auth_data.append(signature);
//		System.out.println("Authorization: " + auth_data);
		return auth_data.toString();
	}

	public static String getSignature(String secretKey,String httpVerb,String contentMd5,String contentType,String date,
			String uri, int encryptionAlgrithm){

		String StringToSign = getStringToSign(httpVerb,contentMd5,contentType,date,uri);

		String encoderData = Base64.encode(getHMACData(StringToSign,secretKey, encryptionAlgrithm));

		return encoderData;
	}


	private static byte[] getHMACData(String data,String secretKey, int encryptionAlgrithm) {

		byte[] byteHMAC = null;
//		String MAC_NAME = GlobalConstant.MAC_NAME;
		String MAC_NAME = null;
//		int encryptionAlgrithm = GlobalConstant.encryptionAlgrithm;
		if (0 == encryptionAlgrithm) {
			MAC_NAME = "HmacSHA1";
		} else if (1 == encryptionAlgrithm) {
			MAC_NAME = "HmacSHA256";
		} else {
			System.out.println("Invalid encryption algrithm");
			return null;
		}

		try{
			Mac mac = Mac.getInstance(MAC_NAME);
			SecretKeySpec spec = new SecretKeySpec(secretKey.getBytes(), MAC_NAME);
			mac.init(spec);

			// 此处保留
	        //System.out.println("UTF-8 before: " + data);
	        //String dataUTF = URLEncoder.encode(data, ENCODING);
	        //System.out.println("UTF-8 after:" + dataUTF);

			byteHMAC = mac.doFinal(data.getBytes());
		}catch(InvalidKeyException e){
			e.printStackTrace();
		}catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}/*catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/

		return byteHMAC;
	}


    /**
     * StringToSign = HTTP-Verb + “\n” +
	 * Content-MD5 + “\n” +
	 *              Content-Type + “\n” +
	 *              Date + “\n” + URI;
	 * @param httpVerb
     * @param contentMd5
     * @param contentType
     * @param date
     * @param uri
     * @return
     */
	private static String getStringToSign (String httpVerb,String contentMd5,String contentType,String date,
			String uri){

		StringBuffer sign_data = new StringBuffer("");

		sign_data.append(httpVerb).append("\n");
		sign_data.append(contentMd5).append("\n");
		sign_data.append(contentType).append("\n");
		sign_data.append(date).append("\n");
		sign_data.append(uri);

		//System.out.println(sign_data.toString() + "\n");
		return sign_data.toString();
	}



}
